package org.shj.basic

import java.io.ByteArrayOutputStream
import java.io.ObjectOutputStream
import java.io.ObjectInputStream
import java.io.ByteArrayInputStream
import scala.io.Source

@SerialVersionUID(88L) class Spark(val name: String) extends Serializable

object SerializeDemo {
  def main(args: Array[String]): Unit = {
    val spark = new Spark("Spark")
    println(deserialize[Spark](serialize[Spark](spark)).name)
    
    
    val filename = "D:\\workspace\\scala\\scala\\file\\employees.json"
    //val url = this.getClass.getResource(filename)
    for(line <- Source.fromFile(filename).getLines()){
      println(line)
    }
    
  }
  
  def serialize[T](o: T) : Array[Byte] = {
    val bos = new ByteArrayOutputStream()
    val oos = new ObjectOutputStream(bos);
    oos.writeObject(o)
    oos.close()
    bos.toByteArray
  }
  
  def deserialize[T](bytes: Array[Byte]): T ={
    val bis = new ByteArrayInputStream(bytes)
    val ois = new ObjectInputStream(bis)
    ois.readObject().asInstanceOf[T]
  }
}